<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      let date = new Date(2015, 0, 2);
      function getDateAgo(date, n) {
        const newDate = new Date(+date);
        const d = newDate.getDate() - n;
        newDate.setDate(d);
        return newDate.getDate();
      }
      console.log(getDateAgo(date, 1)); // 1, (1 Jan 2015)
      console.log(getDateAgo(date, 2)); // 31, (31 Dec 2014)
      console.log(getDateAgo(date, 365)); // 2, (2 Jan 2014)
    </script>
  </body>
</html>
